/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gerenciadoc.utils;

import gerenciadoc.bean.Usuario;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Rafael
 */
public class HibernateUtil {



    private static SessionFactory sessionFactory;

    private HibernateUtil() {

    }

    public static SessionFactory getSessionFactory() {

        if (sessionFactory == null) {
            try {
                // Create the SessionFactory from standard (hibernate.cfg.xml)
                // config file.
                AnnotationConfiguration ac = new AnnotationConfiguration();
                ac.addAnnotatedClass(Usuario.class);
                sessionFactory = ac.configure().buildSessionFactory();
                //SchemaExport se = new SchemaExport(ac);
                //se.create(true, true);


            } catch (Throwable ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }

            return sessionFactory;

        } else {
            return sessionFactory;
        }

    }

//    private static SessionFactory sf;
//    static
//    {
//        try
//        {
//            sf = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
//        }
//        catch (Exception erro)
//        {
//            System.err.println(erro);
//            sf = null;
//        }
//    }
//    public static Session getSession()
//    {
//        return sf.openSession();
//    }
}